package com.netty.controller;

import com.netty.config.NettyConfig;
import com.netty.service.PushMsgService;
import com.netty.vo.ChannelInfoVo;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * <p>
 *
 * </p>
 *
 * @author duguotao
 * @version 1.0.0
 * @since Created in 2022/1/24
 */
@RestController
@RequestMapping("/push")
public class TestController {

    @Autowired
    PushMsgService pushMsgService;

    @GetMapping("/{uid}")
    public void pushOne(@PathVariable String uid) {
        String msg=String.format("hello %s, it's time: %s",uid, LocalDateTime.now());
        pushMsgService.pushMsgToOne(uid, msg);
    }

    @GetMapping("getChannelInfo")
    public List<ChannelInfoVo> getChannelInfo() {
        List<ChannelInfoVo> list=new ArrayList<>();
        ChannelInfoVo channelInfoVo;

        // 遍历ChannelGroup
        for (Map.Entry<String,Channel> entry : NettyConfig.getChannelMap().entrySet()) {
            channelInfoVo=new ChannelInfoVo();
            channelInfoVo.setChannelId(entry.getValue().id().asLongText());
            channelInfoVo.setUserId(entry.getKey());
            list.add(channelInfoVo);
        }

        return list;
    }

}
